﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QL.Core.Log;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;

namespace CM.Resources.Web.Comm.Log 
{
    /// <summary>
    /// RY日志【文件区分日志】,使用GetLogger来获取日志
    /// </summary>
    public class RYLogger : ILogger
    {
        static RYLogger()
        {
            InitLog4Net();
        }

        /// <summary>
        /// 初始化日志
        /// </summary> 
        private static void InitLog4Net()
        {

            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = @"%d{yyyy-MM-dd HH\:mm\:ss} [%p] - %m%n";
            patternLayout.ActivateOptions();

            string currentPath = AppDomain.CurrentDomain.BaseDirectory; 
            string txtLogPath = Path.Combine(currentPath, "Logs", "Log.txt");
       
            FileAppender fileAppender = new Log4NetFileAppender();
            fileAppender.Name = "root";
            fileAppender.File = txtLogPath;
            fileAppender.AppendToFile = true;
        
            fileAppender.Layout = patternLayout;
            fileAppender.Encoding = Encoding.UTF8;
            fileAppender.ActivateOptions();

            ConsoleAppender consoleAppender =new ConsoleAppender();
            consoleAppender.Layout = patternLayout;
            consoleAppender.ActivateOptions();

            BasicConfigurator.Configure(fileAppender);
            BasicConfigurator.Configure(consoleAppender);
        }


        public string LoggerName { get; private set; }
        public ILog CoreLog { get; set; }
        private RYLogger(string loggerName)
            : this(log4net.LogManager.GetLogger(loggerName))
        {
            LoggerName = loggerName;
        }

        private RYLogger(log4net.ILog coreLog)
        {
            CoreLog = coreLog;
        }

        /// <summary>
        /// 记录提示信息日志
        /// </summary>
        /// <param name="message"></param>
        public void Info(string message)
        {
            CoreLog.Info(message);
        }
        /// <summary>
        /// 记录提示信息日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public void Info(string message, params object[] data)
        {
            CoreLog.InfoFormat(message, data);
        }
        /// <summary>
        /// 记录提示信息日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息</param>
        public void Info(Exception ex, string message)
        {
            CoreLog.Info(message, ex);
        }
        /// <summary>
        /// 记录提示信息日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Info(Exception ex, string message, params object[] data)
        {
            CoreLog.Info(string.Format(message, data), ex);
        }


        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message"></param>
        public void Debug(string message)
        {
            CoreLog.Debug(message);
        }
        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public void Debug(string message, params object[] data)
        {
            CoreLog.DebugFormat(message, data);
        }
        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息</param>
        public void Debug(Exception ex, string message)
        {
            CoreLog.Debug(message, ex);
        }
        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Debug(Exception ex, string message, params object[] data)
        {
            CoreLog.Debug(string.Format(message, data), ex);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message"></param>
        public void Error(string message)
        {
            CoreLog.Error(message);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Error(string message, params object[] data)
        {
            CoreLog.ErrorFormat(message, data);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="message"></param>
        public void Error(Exception ex, string message)
        {
            CoreLog.Error(message, ex);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public void Error(Exception ex, string message, params object[] data)
        {
            CoreLog.Error(string.Format(message, data), ex);
        }


        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public void Warn(string message)
        {
            CoreLog.Warn(message);
        }
        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Warn(string message, params object[] data)
        {
            CoreLog.WarnFormat(message, data);
        }
        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息</param>
        public void Warn(Exception ex, string message)
        {
            CoreLog.Warn(message, ex);
        }
        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Warn(Exception ex, string message, params object[] data)
        {
            CoreLog.Warn(string.Format(message, data), ex);
        }

        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        public void Fatal(string message)
        {
            CoreLog.Fatal(message);
        }
        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Fatal(string message, params object[] data)
        {
            CoreLog.FatalFormat(message, data);
        }
        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息</param>
        public void Fatal(Exception ex, string message)
        {
            CoreLog.Fatal(message, ex);
        }
        /// <summary>
        /// 记录致命错误日志
        /// </summary>
        /// <param name="ex">错误异常</param>
        /// <param name="message">日志消息，可以带“{x}”之类的格式符标记，代表附加数据</param>
        /// <param name="data">日志消息里的附加数据</param>
        public void Fatal(Exception ex, string message, params object[] data)
        {
            CoreLog.Fatal(string.Format(message, data), ex);
        }

        static ConcurrentDictionary<string, ILogger> loggerPool = new ConcurrentDictionary<string, ILogger>();
        /// <summary>
        /// 获取日志名称
        /// </summary>
        /// <param name="logName">日志名称</param>
        /// <returns>返回日志记录对象</returns>
        static public ILogger GetLogger(string logName)
        {
            return loggerPool.GetOrAdd(logName, (string key) =>
                  {
                      return new RYLogger(key);
                  });
        }
    }
}
